package com.sds.thesis.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sds.thesis.model.Paper;
import com.sds.thesis.service.TemplateJsonService;
import com.sds.thesis.util.JsonDataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TemplateJsonServiceImpl implements TemplateJsonService {

    private static final Logger log = LoggerFactory.getLogger(TemplateJsonServiceImpl.class);

    @Override
    public Paper handleTemplateJson(String templateName) {
        Paper paper = new Paper();
        JSONObject jsonObject = JsonDataUtil.readJsonFromFile(templateName);

        JSONObject modelObject = jsonObject.getJSONObject("model");
        String projectName = modelObject.getString("projectName");
        paper.setProjectName(projectName);
        log.info("projectName=" + projectName);

        LinkedHashMap<String, String> metaNameMap = getMetaNames(jsonObject);
        paper.setMetaNameMap(metaNameMap);

        // 获取主要功能模块
        String primaryService = String.join("、", metaNameMap.keySet());
        paper.setPrimaryService(primaryService);

        // 获取主要管理模块
        String primaryServiceManage = metaNameMap.keySet().stream()
                .map(key -> key + "管理")
                .collect(Collectors.joining("、"));
        paper.setPrimaryServiceManage(primaryServiceManage);

        // 获取主要模块信息表
        String primaryServiceInfoTable = metaNameMap.keySet().stream()
                .map(key -> key + "信息表")
                .collect(Collectors.joining("、"));
        paper.setPrimaryServiceInfoTable(primaryServiceInfoTable);

        // 获取主要模块接口
        String primaryServiceInterface = metaNameMap.keySet().stream()
                .map(key -> key + "接口")
                .collect(Collectors.joining("、"));
        paper.setPrimaryServiceInterface(primaryServiceInterface);

        // 获取主要模块管理模块
        String primaryServiceManageModule = metaNameMap.keySet().stream()
                .map(key -> "管理" + key + "模块")
                .collect(Collectors.joining("、"));
        paper.setPrimaryServiceManageModule(primaryServiceManageModule);

        // 获取所有的metaPath
        String primaryServiceManagePath = String.join("、", metaNameMap.values());
        log.info("primaryServiceManagePath=" + primaryServiceManagePath);

        JSONArray tableList = modelObject.getJSONArray("tableList");
        paper.setTableList(tableList);

        log.info("jsonObject=" + jsonObject);
        return paper;
    }

    /**
     * 获取所有功能模块
     *
     * @param jsonObject
     * @return
     */
    private LinkedHashMap<String, String> getMetaNames(JSONObject jsonObject) {
        JSONArray menuList = jsonObject.getJSONObject("model").getJSONArray("menuList");
        LinkedHashMap<String, String> result = new LinkedHashMap<>();
        for (int i = 0; i < menuList.size(); i++) {
            JSONObject menu = menuList.getJSONObject(i);
            if (menu.getInteger("menuType") == 2 && !menu.getString("metaPath").isEmpty()) {
                result.put(menu.getString("metaName").replace("管理",""), menu.getString("metaPath"));
            }
            if (menu.containsKey("children")) {
                JSONArray children = menu.getJSONArray("children");
                for (int j = 0; j < children.size(); j++) {
                    JSONObject child = children.getJSONObject(j);
                    if (child.getInteger("menuType") == 2 && !child.getString("metaPath").isEmpty()) {
                        result.put(child.getString("metaName").replace("管理",""), child.getString("metaPath"));
                    }
                }
            }
        }
        return result;
    }
}
